package leetCode.thread.leetCode1117;

import java.util.concurrent.Semaphore;

public class H2O1 implements H2O {

    //信号量实现线程协作
    Semaphore hSem = new Semaphore(0);
    Semaphore oSem = new Semaphore(2);

    public void hydrogen(Runnable releaseHydrogen) throws InterruptedException {
        oSem.acquire();
        releaseHydrogen.run();
        hSem.release();
    }

    public void oxygen(Runnable releaseOxygen) throws InterruptedException {
        hSem.acquire(2);
        releaseOxygen.run();
        oSem.release(2);
    }

}
